﻿using System;
using System.IO;
using System.Text;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Microsoft.MetadirectoryServices;

namespace FIM.ECMA2.FileBased.Powershell
{
    public class EzmaExtension :
    //IMAExtensible2CallExport,
    //IMAExtensible2CallImport,
    IMAExtensible2FileImport,
    IMAExtensible2FileExport,
    //IMAExtensible2GetHierarchy,
    //IMAExtensible2GetSchema,
    IMAExtensible2GetCapabilities,
    IMAExtensible2GetParameters
    //IMAExtensible2GetPartitions

    {

        private struct myParamStrings
        {
            public const string PowershellFileName = "Powershell File Name";
            public const string treatWarningsAsError = "Treat warnings as error";
            public const string forceLocalRunSpace = "Run powershell localy";
            public const string ExportEncoding = "Export Encoding";
            public const string ExportEncodingOptions = "ASCII,Unicode,UTF8";
            public const string ExportEncodingDefaultOption = "UTF8";
        }

        //
        // Constructor
        public EzmaExtension()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public MACapabilities Capabilities
        {
            get
            {
                MACapabilities myCapabilities = new MACapabilities();

                myCapabilities.ConcurrentOperation = true;
                myCapabilities.ObjectRename = true;
                myCapabilities.DeleteAddAsReplace = true;
                myCapabilities.DeltaImport = true;
                myCapabilities.DistinguishedNameStyle = MADistinguishedNameStyle.Generic;
                myCapabilities.ExportType = MAExportType.AttributeUpdate;
                myCapabilities.NoReferenceValuesInFirstExport = false;
                myCapabilities.Normalizations = MANormalizations.None;
                myCapabilities.FullExport = true;

                return myCapabilities;
            }
        }

        public IList<ConfigParameterDefinition> GetConfigParameters(KeyedCollection<string, ConfigParameter> configParameters,
                                                             ConfigParameterPage page)
        {
            List<ConfigParameterDefinition> configParametersDefinitions = new List<ConfigParameterDefinition>();

            switch (page)
            {
                case ConfigParameterPage.Connectivity:
                    configParametersDefinitions.Add(ConfigParameterDefinition.CreateStringParameter(myParamStrings.PowershellFileName, ""));
                    break;


                case ConfigParameterPage.Global:
                   
                   //configParametersDefinitions.Add(ConfigParameterDefinition.CreateDropDownParameter(myParamStrings.ExportEncoding, myParamStrings.ExportEncodingOptions, false, myParamStrings.ExportEncodingDefaultOption));
                   break;

                case ConfigParameterPage.Partition:
                    break;

                case ConfigParameterPage.RunStep:
                        //configParametersDefinitions.Add(ConfigParameterDefinition.CreateCheckBoxParameter(myParamStrings.forceLocalRunSpace, false));
                        //configParametersDefinitions.Add(ConfigParameterDefinition.CreateStringParameter(myParamStrings.PowershellFileName,""));
                        //configParametersDefinitions.Add(ConfigParameterDefinition.CreateCheckBoxParameter(myParamStrings.treatWarningsAsError, false));
                    break;
            }

            return configParametersDefinitions;
        }

        public ParameterValidationResult ValidateConfigParameters(KeyedCollection<string, ConfigParameter> configParameters,
                                                                   ConfigParameterPage page)
        {


            ParameterValidationResult myResults = new ParameterValidationResult();
            return myResults;

        }

        public WriteImportFileResults WriteImportFile(
                                             KeyedCollection<string, ConfigParameter> configParameters,
                                             Schema types,
                                             WriteImportFileRunStep importRunStep)
        {
            //
            // Write out the delimited file.
            //
            StreamWriter swImport = new StreamWriter(importRunStep.FilePath, true);

            string MAFolder = MAUtils.MAFolder;
            string myFile=null;
            
            switch (importRunStep.ImportType)
            {
                case OperationType.Full:
                    //myFile = MAUtils.MAFolder +@"\" + configParameters["Full Import File"].Value;
                    break;

                case OperationType.FullObject:
                    //myFile = MAUtils.MAFolder +@"\" + configParameters["Full Object Import File"].Value;
                    break;

                case OperationType.Delta:
                    //myFile = MAUtils.MAFolder +@"\" + configParameters["Delta Import File"].Value;
                    break;

            }
            
            //call powershell

            //get result objects

            //write result to importfile
            //string fullline = objectclass + "," + delta + "," + anchor + "," + name + "," + email;

            swImport.WriteLine(myFile);

            swImport.WriteLine();

            swImport.Close();

            return new WriteImportFileResults();
        }

        public void ReadExportFile(KeyedCollection<string, ConfigParameter> configParameters,
                                     Schema types,
                                     ReadExportFileRunStep exportRunStep)
        
        {
            //throw new EntryExportException("Galet fel någonstans");
        }

    }
}